package com.itheima.tlias84.controller;

import com.itheima.tlias84.Po.InsertStudentParam;
import com.itheima.tlias84.Po.SelectStudentPageParam;
import com.itheima.tlias84.Service.StudentService;
import com.itheima.tlias84.Util.ResultUtil;
import com.itheima.tlias84.dto.SelectStudentDTO;
import com.itheima.tlias84.dto.SelectStudentDegreeDataDTO;
import com.itheima.tlias84.entity.Student;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/students")
public class StudentController {
    @Resource
    StudentService service;

    //根据id查询学生
    @GetMapping("{id}")
    public ResultUtil selectStudentById(@PathVariable("id") Integer id){
        SelectStudentDTO dto = service.selectStudentById(id);
        return ResultUtil.success(dto);
    }

    //分页查询学生
    @GetMapping
    public ResultUtil selectStudentByPage(SelectStudentPageParam param){
        if (Objects.isNull(param.getPage()) || Objects.isNull(param.getPageSize())) {
            return ResultUtil.fail("参数错误");
        }
        return service.selectStudentByPage(param);
    }

    //新增学生
    @PostMapping
    public ResultUtil insertStudent(@RequestBody InsertStudentParam param){
        int i = service.insertStudent(param);
        return i==0?ResultUtil.fail("新增失败"):ResultUtil.success("新增成功");
    }


    @DeleteMapping
    public ResultUtil deleteById(Integer id ){
        int i = service.deleteById(id);
        return i==0?ResultUtil.fail("删除失败"):ResultUtil.success("删除成功");
    }

    @PutMapping
    public ResultUtil updateStudentById(@RequestBody Student student){
      return service.updateStudentById(student);
    }

    @PutMapping("/violation/{id}/{score}")
    public ResultUtil updateStudentViolation(@PathVariable ("id")Integer id ,@PathVariable("score") Integer score){
        System.out.println(id+" "+score);
        System.out.println("=========================");
        int i = service.updateStudentViolation(id, score);
        return i==0?ResultUtil.fail("更新违纪分数失败"):ResultUtil.success("更新违纪分数成功");
    }





}
